@echo off

setlocal enabledelayedexpansion

call ..\setEnv.bat

set /a is_err_if_lines=2

set info_m=
set /a ret=0

:renew

if exist drop_dest_db.log del drop_dest_db.log
if exist spid.txt del spid.txt

echo stop app server started...
call stop_app_server.bat
echo stop app server completed...

set NLS_LANG=AMERICAN_AMERICA.UTF8

sqlplus %dest_admin_dbuser%/%dest_admin_dbpwd%@%dest_admin_tnsname% @select_spid_dest_db.sql

if not exist spid.txt (
  echo no spid.txt exist, spid cannot be retrieve, please check.
  set info_m=with errors.
  set /a ret=-100
  goto finish
)


echo kill remote session started...
call kill_remote_session.bat
echo kill remote session completed...


sqlplus %dest_admin_dbuser%/%dest_admin_dbpwd%@%dest_admin_tnsname% @drop_dest_db.sql


if exist 1.err_counts del 1.err_counts
if exist 2.err_counts del 2.err_counts

if not exist drop_dest_db.log (
  echo drop_dest_db.log does not exist, maybe drop_dest_db does not execute, please check it.
  set info_m=with errors.
  set /a ret=-100
  goto finish
)

for %%i in (drop_dest_db.log) do (

set logfile=%%i
echo now check !logfile! ... ...

find /i "ORA-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
find /i "ORA-00031" !logfile! > 2.err_counts
set /a vars_1=0
for /f %%k in (2.err_counts) do set /a vars_1=!vars_1!+1
if !vars_1! GEQ %is_err_if_lines% (
  echo [warning]session killed delay, goto renew killed cycle.
  goto renew
) else ( 
  if !vars! GEQ %is_err_if_lines% (
    goto dbscriptError_ex
  ) else (
    del /q 1.err_counts
    del /q 2.err_counts
  ) 
)


find /i "SP2-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "PLS-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "IMP-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "EBER-" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i "DBERROR:" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)

find /i """O/S Message:""" !logfile! > 1.err_counts
set /a vars=0
for /f %%j in (1.err_counts) do set /a vars=!vars!+1
if !vars! GEQ %is_err_if_lines% (goto dbscriptError_ex) else (del /q 1.err_counts)
)



:succ_1
echo check drop_dest_db.log  completed ... ...
set info_m=without errors.
set /a ret=100
goto finish

:dbscriptError_ex
echo !logfile! has errors.
set info_m=with errors.
set /a ret=-100

:finish
echo finish !info_m!

exit !ret!

endlocal


